<?php
defined('_JEXEC') or die('Restricted access');
//JToolBarHelper::title(JText::_('NS Pro Manager'), 'user.png');

$db =& JFactory::getDBO();
$db->setQuery('SELECT `id`, `lname` FROM `#__nspro_lists`');
$mlists = $db->loadObjectList();

$db->setQuery('SELECT * FROM `#__nspro_fields`');
$afields = $db->loadObjectList();

$fieldValues = array();
if (!$this->isNew) {
  $db->setQuery('SELECT * FROM `#__nspro_field_values` WHERE `subscriber_id` = "' . $this->sub->id . '"');
  $fieldValues = $db->loadObjectList();
}

if (!function_exists('GetFieldInputSubForm')) {
  include('components/com_nspro/lib.php');
}

$jversion = new JVersion();
$j15 = ($jversion->RELEASE == '1.5');
//$j16 = ($jversion->RELEASE == '1.6');

?>
<script type="text/javascript"><!--

  <?php if ($j15 == true) { ?>
  function submitbutton(task) {
  <?php } else { ?>
  Joomla.submitbutton = function(task) {
  <?php } ?>
    var form = document.adminForm;

    if (task == 'cancel') {
      <?php if ($j15 != true) { ?>Joomla.<?php } ?>submitform( task );
      return;
    }

    selRef = document.getElementById('mailing_lists2');
    var mstring = '';
    for (var i=0; i < selRef.options.length;i++) {
      if (selRef.options[i].selected) {
        if (mstring == '') {
          mstring = selRef.options[i].value;
        }
        else  {
          mstring = mstring + ',' + selRef.options[i].value;
        }
      }
    }

    document.getElementById('mailing_lists').value = mstring;
    <?php if ($j15 != true) { ?>Joomla.<?php } ?>submitform( task );
  }

--></script>
<form action="index.php" method="post" name="adminForm" id="adminForm">
<div class="col width-10 <?php if ($j15 != true) { print 'fltlft'; } ?>">
    <fieldset class="adminform">
        <legend><?php echo JText::_( 'Details' ); ?></legend>

        <table class="admintable <?php if ($j15 != true) { print 'adminform'; } ?>">
        <tr>
            <td width="100" class="key">
                <label for="name">
                    <?php echo JText::_( 'Name' ); ?>:
                </label>
            </td>
            <td>
                <input class="text_area" type="text" name="name" id="name" size="60 " maxlength="250"
                value="<?php echo $this->sub->name;?>" />
            </td>
        </tr>
        <tr>
            <td width="110" class="key">
                <label for="url">
                    <?php echo JText::_( 'Email' ); ?>:
                </label>
            </td>
            <td>
                <input class="inputbox" type="text" name="email" id="email" size="60" value="<?php echo $this->sub->email; ?>" />
            </td>
        </tr>
        <tr>
            <td width="110" class="key">
                <label for="datetime">
                    <?php echo JText::_( 'Date and Time' ); ?>:
                </label>
            </td>
            <td>
                <input class="inputbox" type="text" name="datetime" id="datetime" size="60" value="<?php echo $this->sub->datetime; ?>" />
            </td>
        </tr>
        <tr>
            <td width="120" class="key">
                <?php echo JText::_( 'Confirmed' ); ?>:
            </td>
            <td><?php if ($j15 != true) { print '<fieldset class="radio inputbox">'; } ?>
                <?php echo JHTML::_( 'select.booleanlist',  'confirmed', 'class="inputbox"', $this->sub->confirmed ); ?>
                <?php if ($j15 != true) { print '</fieldset>'; } ?>
            </td>
        </tr>
        <tr>
            <td width="110" class="key">
                <label for="mailing_lists">
                    <?php echo JText::_( 'Mailing Lists' ); ?>:
                </label>
            </td>
            <td>
                <input type="hidden" name="mailing_lists" id="mailing_lists" value="<?php echo $this->sub->mailing_lists; ?>" />
                <select class="inputbox" size="5" style="width: 258px;" multiple="multiple" name="mailing_lists2[]" id="mailing_lists2">
                <?php
                foreach($mlists as $mlist) {
                  print '<option value="' . $mlist->id . '" id="list_' . $mlist->id . '"';
                  print '>' . $mlist->lname . '</option>';
                }
                ?>
                </select>
            </td>
        </tr>
        <tr>
            <td width="110" class="key">
                <label for="notes">
                    <?php echo JText::_( 'Notes' ); ?>:
                </label>
            </td>
            <td>
                <input class="inputbox" type="text" name="notes" id="notes" size="60" value="<?php echo $this->sub->notes; ?>" />
            </td>
        </tr>
        <?php

        foreach($afields as $afield) {
          print '<tr>';
          print '<td width="110" class="key">';
          print '  <label for="field' . $afield->id . '">';
          print    JText::_($afield->name);
          print '  </label>';
          print '</td>';
          print '<td>';
          $val = '';
          if (!$this->isNew) {
            foreach($fieldValues as $fv) {
              if ($fv->field_id == $afield->id) {
                $val = $fv->value;
                break;
              }
            }
          }
          $fieldinput = GetFieldInputSubForm($afield, $this->isNew, $val);
          print $fieldinput;
          print '</td>';
          print '</tr>';
        }
        ?>
        </table>
    </fieldset>
</div>
<div class="clr"></div>

<input type="hidden" name="option" value="com_nspro" />
<input type="hidden" name="id" value="<?php echo $this->sub->id; ?>" />
<input type="hidden" name="task" value="" />
<input type="hidden" name="controller" value="nspro" />
</form>

<small><i>NS Pro by <a href="http://www.mavrosxristoforos.com/">Mavrosxristoforos.com</a> Copyright 2009-2011</i></small>
<script type="text/javascript"><!--
<?php
$mSubLists = explode(',', $this->sub->mailing_lists);
foreach ($mSubLists as $msublist) {
  if ($msublist != '') {
    print 'if (document.getElementById("list_'.$msublist.'")) {' . "\n";
    print '  document.getElementById("list_'.$msublist.'").setAttribute("selected","selected");' . "\n";
    print '}' . "\n";
  }
}
?>
-->
</script>